<?php
declare (strict_types = 1);

namespace app\middleware;

class CommonKey
{
    /**
     * 公共请求验证处理
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        if(config('app')['role_open']){
            $herd = $request->header();
            if(!isset($herd['time']) || !isset($herd['token']) || !isset($herd['user'])){
                exit(re_error('非法请求'));
            }
            if(md5($herd['time'].md5(config('app')['role_key'])) !== $herd['token']){
                exit(re_error('验权失败'));
            }
        }
        return $next($request);
    }
}
